package com.excu.utils;


import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Xing Dao Rong
 * @since 2021/9/17 14:00
 * 字符串工具类
 */
public class StringUtil extends StringUtils implements Serializable {
    private static final long serialVersionUID = 3375173776417938676L;


    /**
     * 字符串转集合
      * @param str 1 1
     * @return 返回
     */
    public static List<String> strToList(String str) {
        String[] strs = {};
        if (str.contains(",")) {
            strs = str.split(",");
        } else {
            strs = new String[]{str};
        }
        return Arrays.asList(strs);
    }

    /**
     * 集合转字符串
     * @param strs 1
     * @param sym 1
     * @return 返回
     */
    public static String listToStr(List<String> strs,String sym) {
        return String.join(sym,strs);
    }




    /**
     * 集合是否含有某个字符串
      * @param str 1  1
     * @param list 1
     * @return 返回
     */
    public static boolean listIsContainsStr(String str, List<String> list) {
        Set<String> set = new HashSet<>(list);
        return set.contains(str);
    }


    /**
     * @param str 1
     * @return  1
     * 下划线转驼峰命名
     */
    public static String toUnderScoreCase(String str) {
        if (str == null) {
            return null;
        }
        if (!str.contains("_")) {
            return str;
        }
        StringBuilder sb = new StringBuilder();
        // 前置字符是否大写
        boolean preCharIsUpperCase = true;
        // 当前字符是否大写
        boolean curreCharIsUpperCase = true;
        // 下一字符是否大写
        boolean nexteCharIsUpperCase = true;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (i > 0) {
                preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
            } else {
                preCharIsUpperCase = false;
            }

            curreCharIsUpperCase = Character.isUpperCase(c);

            if (i < (str.length() - 1)) {
                nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
            }

            if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase) {
                sb.append("_");
            } else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase) {
                sb.append("_");
            }
            sb.append(Character.toLowerCase(c));
        }

        return sb.toString();
    }


    /**
     * 下划线转转驼峰
      * @param str 1
     * @return 返回
     */
    public static String getUp(String str) {
        if (!str.contains("_")) {
            return str;
        }
        Pattern linePattern = Pattern.compile("_(\\w)");
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 驼峰法转下划线
      * @param line 源字符串
     * @param b 1
     * @return 返回 转换后的字符串
     */
    public static String camelToUnderline(String line,boolean b) {
        if (!b){
            return line;
        }
        if (line == null || "".equals(line)) {
            return "";
        }
        char[] chars = line.toCharArray();
        int rs = 0;
        for (char aChar : chars) {
            if (65 <= aChar && aChar <= 90) {
                rs = 1;
                break;
            }
        }
        if (rs == 0) {
            return line;
        }
        line = String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
        StringBuffer sb = new StringBuffer();
        Pattern pattern = Pattern.compile("[A-Z]([a-z\\d]+)?");
        Matcher matcher = pattern.matcher(line);
        while (matcher.find()) {
            String word = matcher.group();
            sb.append(word.toUpperCase());
            sb.append(matcher.end() == line.length() ? "" : "_");
        }
        return sb.toString().toLowerCase();
    }


    public static String changeType(String str) {
        Matcher mat = Pattern.compile("(?<=\\()(\\S+)(?=\\))").matcher(str);//此处是中文输入的()
        while (mat.find()) {
            str = str.replace("(" + mat.group() + ")", "");
        }
        return str;
    }

    /**
     * 获取实体类
      * @param str 1
     * @return 返回
     */
    public static String getClass(String str) {
        str = getUp(str);
        String[] var1 = {"bigint", "varchar", "datetime", "char", "int", "double"};
        String[] var2 = {"Long", "String", "Date", "String", "Integer", "Double"};
        for (int i = 0; i < var1.length; i++) {
            str = str.replace(var1[i], var2[i]);
        }
        str = changeType(str).replace("/;", "/\n");
        return str;
    }

    /**
     * 获取名字
      * @param tableName 1
     * @return 返回
     */
    public static String getClassName(String tableName) {
        return getUp(tableName).substring(0, 1).toUpperCase() + getUp(tableName).substring(1);
    }

    /**
     * 转换类型
      * @param type 1
     * @return 返回
     */
    public static String changeType2(String type) {
        String[] var1 = {"bigint", "varchar", "datetime", "char", "int", "text", "decimal", "date", "tinyint"};
        String[] var2 = {"Long", "String", "Date", "String", "Integer", "String", "BigDecimal", "Date", "Integer"};
        for (int i = 0; i < var1.length; i++) {
            if (type.equals(var1[i])) {
                return var2[i];
            }
        }
        return "String";
    }

    /**
     * 拼接字符
      * @param prefix 1
     * @param strs 1s
     * @param suffix 1
     * @param g 1
     * @return 返回
     */
    public static String appendStr(String prefix, List<String> strs, String suffix, String g) {
        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder prefixBuilder = stringBuilder.append(prefix);
        if (strs.isEmpty()) {
            return prefixBuilder.append(suffix).toString();
        }
        for (int i = 0; i < strs.size(); i++) {
            if (StringUtils.isNoneEmpty(strs.get(i))) {
                prefixBuilder.append(strs.get(i));
            }
            if (StringUtils.isNoneEmpty(g) && i != strs.size() - 1) {
                prefixBuilder.append(g);
            }
        }
        return prefixBuilder.append(suffix).toString();
    }







    /**
     * 字符匹配多个
      * @param strs 1s
     * @return 返回s
     */
    public static boolean equalsIgnoreCase(String... strs) {
        boolean flag = false;
        for (int i = 1; i < strs.length; i++) {
            if (strs[0].equalsIgnoreCase(strs[i])) {
                flag = true;
                break;
            }
        }
        return flag;
    }



    /**
     * 加密
      * @param password 1
     * @return 返回
     */
    public static String encryptionPassword(String password) {
        int len = password.length();
        char[] chars = password.toCharArray();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < chars.length; i++) {
            char condiment = (char) ((len - i) % 7);
            if (len % 2 == 0) {
                builder.append(chars[i]).append(condiment);
            } else {
                builder.append(condiment).append(chars[i]);
            }
        }
        return builder.toString();
    }

    /**
     * 解密
      * @param password 1
     * @return 返回
     */
    public static String decryptPassword(String password) {
        int len = password.length() / 2;
        char[] chars = password.toCharArray();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < chars.length; i++) {
            if (len % 2 == 0) {
                if (i % 2 == 0) {
                    builder.append(chars[i]);
                }
            } else {
                if (i % 2 == 1) {
                    builder.append(chars[i]);
                }
            }
        }
        return builder.toString();
    }
}
